package com.china.springServer.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @fileName: CustomerThreadPoolConfig
 * @author: LmR
 * @createDate: 2023/10/22 9:22
 * @Desc:
 */
@Configuration
public class CustomerThreadPoolConfig {

    @Value("${spring.task.execution.pool.core-size}")
    private int corePoolSize;
    @Value("${spring.task.execution.pool.max-size}")
    private int maxPoolSize;
    @Value("${spring.task.execution.pool.keep-alive}")
    private int keepAliveTime;
    @Value("${spring.task.execution.pool.queue-capacity}")
    private int queueCapacity;
    @Value("${spring.task.execution.pool.allow-core-thread-timeout}")
    private boolean allowCoreThreadTimeout;

    @Bean("customerThreadPool")
    public ExecutorService getThreadPoolExecutor() {
        /**
         * 自定义线程池
         */
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(queueCapacity),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        executor.allowCoreThreadTimeOut(allowCoreThreadTimeout);
        return executor;
    }

}
